/**
 * Title: AreaDao.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-1-8 下午5:06:36
 * Version 1.0
 */
package com.luoxudong.phonenumber.logic.db.dao;

import java.util.List;

import com.luoxudong.phonenumber.logic.db.model.AreaInfo;
import com.luoxudong.phonenumber.service.database.manager.BaseDao;

/** 
 * ClassName: AreaDao
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-1-8 下午5:06:36
 */
public class AreaDao extends BaseDao<AreaInfo, Long>{
private static final String TABLE_NAME = "t_area";
	
	private static final String CREATE_TABLE = "create table if not exists " + TABLE_NAME
			+ "(" 
			+ "id integer primary key autoincrement,"
			+ "area_id integer not null,"
			+ "parent_area_id integer not null,"
			+ "area_name text not null," 
			+ " unique (area_id) ON CONFLICT  replace)";
	
	@Override
	protected String getCreateTableSql() {
		return CREATE_TABLE;
	}

	@Override
	protected String getTableName() {
		return TABLE_NAME;
	}
	
	public List<AreaInfo> getAreaInfoByParentId(int parentId) {
		String sql = "select * from " + TABLE_NAME + " where parent_area_id=? order by area_id";
		
		List<AreaInfo> areaInfos = query(sql, new String[]{String.valueOf(parentId)}, AreaInfo.class);
		
		return areaInfos;
	}
	
	public long getAreaInfoCountByParentId(int parentId) {
		String sql = "select count(id) from " + TABLE_NAME + " where parent_area_id=?";
		
		long count = queryCount(sql, new String[]{String.valueOf(parentId)});
		
		return count;
	}
	
	public AreaInfo getAreaInfoByAreaId(int areaId)
	{
		String sql = "select * from " + TABLE_NAME + " where area_id=?";
		
		List<AreaInfo> areaInfos = query(sql, new String[]{String.valueOf(areaId)}, AreaInfo.class);
		
		if (areaInfos != null && areaInfos.size() > 0)
		{
			return areaInfos.get(0);
		}
		
		return null;
	}
}
